package com.supor.aiot.mine.ui.adapter;

import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.android.baseconfig.base.GlideApp;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.supor.aiot.common.utils.NotNullUtils;
import com.supor.aiot.mine.R;
import com.supor.aiot.mine.bean.CookingRecordBean;
import com.supor.aiot.mine.bean.CookingRecordItemBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: zook
 * @date: 2021/10/15
 */
public class CookingRecordAdapter extends BaseMultiItemQuickAdapter<CookingRecordItemBean, BaseViewHolder> {
    public CookingRecordAdapter() {
        super(new ArrayList<>());
//        addItemType(CookingRecordItemBean.TYPE_YEAR, R.layout.item_cooking_year_title);
        addItemType(CookingRecordItemBean.TYPE_MONTH, R.layout.item_cooking_month_title);
        addItemType(CookingRecordItemBean.TYPE_ITEM, R.layout.item_cooking_record_comm);
        addChildClickViewIds(R.id.tv_eval, R.id.tv_cooking_copy);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, CookingRecordItemBean item) {
        switch (item.getItemType()) {
//            case CookingRecordItemBean.TYPE_YEAR:
//                holder.setText(R.id.tv_title, item.getLocal_time().split("-")[0]);
//                break;
            case CookingRecordItemBean.TYPE_MONTH:
                String[] date = item.getLocal_time().split("-");
                holder.setText(R.id.tv_day, date[2]);
                holder.setText(R.id.tv_month_day, date[0] + "年" + date[1] + "月" + date[2] + "日");
                holder.setText(R.id.tv_prcount_times, "烹饪" + item.getLocal_prcount() + "次");
                break;
            case CookingRecordItemBean.TYPE_ITEM:
                GlideApp.with(getContext())
                        .load(item.getFoodTRecipeVo().getSmallLandscape())
                        .into((ImageView) holder.getView(R.id.iv_img));
                holder.setText(R.id.tv_recipe_title, NotNullUtils.getNotNullValue(item.getFoodTRecipeVo().getRecipeName()))
                        .setText(R.id.tv_device_name, NotNullUtils.getNotNullValue(item.getFoodTRecipeVo().getCrmModelName()))
                        .setText(R.id.tv_cooking_times, "累计烹饪" + item.getFoodTRecipeVo().getPrCount() + "次")
                        .setGone(R.id.tv_eval, item.getIsEval() == 1)
                ;

                try {
                    String[] strs = item.getCreateTime().split(" ");
                    int timeNum = Integer.parseInt(strs[1].replaceAll(":", ""));
                    System.out.println("time_debug: strs=" + strs[1] + " timeNum=" + timeNum + "  " + item.getFoodTRecipeVo().getRecipeName());
//                    "00:00:00"
//                    "10:00:00"
//                    "14:00:00"
//                    "21:00:00"
                    if (timeNum < 100000) {
                        holder.setText(R.id.tv_tag, "早餐");
                        holder.setBackgroundResource(R.id.tv_tag, R.drawable.shape_cooking_tag_noon);
                        System.out.println("checkDateTag_debug : " + timeNum + "   早餐");
                    } else if (timeNum < 140000) {
                        holder.setText(R.id.tv_tag, "午餐");
                        holder.setBackgroundResource(R.id.tv_tag, R.drawable.shape_cooking_tag_noon);
                        System.out.println("checkDateTag_debug : " + timeNum + "   午餐");

                    } else if (timeNum < 210000) {
                        holder.setText(R.id.tv_tag, "晚餐");
                        holder.setBackgroundResource(R.id.tv_tag, R.drawable.shape_cooking_tag_morning);
                        System.out.println("checkDateTag_debug : " + timeNum + "   晚餐");

                    } else {
                        holder.setText(R.id.tv_tag, "夜宵");
                        holder.setBackgroundResource(R.id.tv_tag, R.drawable.shape_cooking_tag_morning);
                        System.out.println("checkDateTag_debug : " + timeNum + "   夜宵");

                    }

//                    int timeType = checkDateTag(new SimpleDateFormat("HH:mm:ss").parse(strs[1]));
//                    System.out.println("checkDateTag_debug:date = " + new SimpleDateFormat("HH:mm:ss").parse(strs[1])
//                            + "timeType = " + timeType);
//
//                    switch (timeType) {
//                        case 1:
//                            holder.setText(R.id.tv_tag, "早餐");
//                            holder.setBackgroundResource(R.id.tv_tag, R.drawable.shape_cooking_tag_noon);
//                            break;
//                        case 2:
//                            holder.setText(R.id.tv_tag, "午餐");
//                            holder.setBackgroundResource(R.id.tv_tag, R.drawable.shape_cooking_tag_noon);
//                            break;
//                        case 3:
//                            holder.setText(R.id.tv_tag, "晚餐");
//                            holder.setBackgroundResource(R.id.tv_tag, R.drawable.shape_cooking_tag_morning);
//                            break;
//                        case 4:
//                            holder.setText(R.id.tv_tag, "夜宵");
//                            holder.setBackgroundResource(R.id.tv_tag, R.drawable.shape_cooking_tag_morning);
//                            break;
//                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


                break;
        }
    }

    /*
     * 1 早餐 2 午餐 3 晚餐 4 夜宵
     * */
    private int checkDateTag(Date nowTime) {
        int type = 1;
        String format = "HH:mm:ss";
        try {
            Calendar now = Calendar.getInstance();
            now.setTime(nowTime);

            Calendar time1 = Calendar.getInstance();
            time1.setTime(new SimpleDateFormat(format).parse("00:00:00"));
            Calendar time2 = Calendar.getInstance();
            time2.setTime(new SimpleDateFormat(format).parse("10:00:00"));
            Calendar time3 = Calendar.getInstance();
            time3.setTime(new SimpleDateFormat(format).parse("14:00:00"));
            Calendar time4 = Calendar.getInstance();
            time4.setTime(new SimpleDateFormat(format).parse("21:00:00"));
            if (now.after(time1) && now.before(time2)) {
                type = 1;
            } else if (now.after(time2) && now.before(time3)) {
                type = 2;
            } else if (now.after(time3) && now.before(time4)) {
                type = 3;
            } else if (now.after(time4) && now.before(time1)) {
                type = 4;
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return type;
    }
}